import pickle
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Model(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    group = db.Column(db.String(50))
    name = db.Column(db.String(50))
    model = db.Column(db.String(50))
    package = db.Column(db.String(50))
    data = db.Column(db.LargeBinary)

    def __init__(self, name,model,group,package, data=None):
        self.name = name
        self.model = model
        self.package = package
        self.group = group
        if data != None:
            self.data = pickle.dumps(data)

    def get_data(self):
        return pickle.loads(self.data)

class Data(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    group = db.Column(db.String(50))
    name = db.Column(db.String(50))
    data = db.Column(db.LargeBinary)

    def __init__(self, name,group, data=None):
        self.name = name
        self.group = group
        if data is not None:
            self.data = pickle.dumps(data)

    def get_data(self):
        return pickle.loads(self.data)
    
class User(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    username = db.Column(db.String(50))
    password = db.Column(db.String(50))
    
    def __init__(self, username,password):
        self.username = username
        self.password = password
        
    def __str__(self):
        return "User(id='%s')" % self.id
    
    def __getitem__(self,value):
        return getattr(self,value)